# import bezier_curve_smooth as bz
import matplotlib.pyplot as plt
import numpy as np
_points = [(0, 10), (20, 30), (40, 20)]


def my_bezier_curve(points, num=100):
    t = np.linspace(0, 1, num)
    # 获取第一个点和第三个点的坐标
    x1 = points[0][0]
    y1 = points[0][1]
    x2 = points[1][0]
    y2 = points[1][1]
    x3 = points[2][0]
    y3 = points[2][1]

    x_list = []
    y_list = []
    for i in range(100):
        x = x1*((1-t[i])**2) + 2*(1-t[i])*t[i]*x2 + x3*t[i]**2
        y = y1*((1-t[i])**2) + 2*(1-t[i])*t[i]*y2 + y3*t[i]**2
        x_list.append(x)
        y_list.append(y)

    curve_points = np.column_stack((x_list, y_list))
    return curve_points


piecewise_curve = my_bezier_curve(_points, 100)
plt.plot(*zip(*_points), marker='o', color='blue', label='broken line')
plt.plot(piecewise_curve[:, 0], piecewise_curve[:, 1], marker='', color='red', label='bezier curve')


plt.legend()

plt.title('Second-Order Bezier Curve')
plt.xlabel('x')
plt.ylabel('y')

plt.grid(True)
plt.axis('equal')
plt.show()
